package com.contractpage.ec;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.contractpage.common.persistence.Page;
import com.contractpage.ec.entity.ContractDetail;
import com.contractpage.ec.entity.ExpressInformation;
import com.contractpage.ec.entity.Resource;
import com.contractpage.ec.service.ContractDetailService;
import com.contractpage.util.FileEntity;
import com.contractpage.util.FileUtil;
import com.contractpage.util.ResourceFile;
import com.contractpage.util.StringUtils;

@Controller
@RequestMapping(value = "contractDetail")
public class ContractDetailController extends BaseController{

	@Autowired
	private ContractDetailService contractDetailService;

	@RequestMapping(value = "findList")
	@ResponseBody
	public Object findList(ContractDetail entity, HttpServletRequest request, HttpServletResponse response, Model model){
		Page<ContractDetail> page = new Page<ContractDetail>(request,response);
		return  contractDetailService.findList(page, entity);
	}
	@RequestMapping(value = "canselDistribute")
	@ResponseBody
	public Object canselDistribute(ContractDetail entity, HttpServletRequest request, HttpServletResponse response, Model model){
		setUser(entity);
		return contractDetailService.canselDistribute(entity);
	}
	@RequestMapping(value = "canseContract")
	@ResponseBody
	public Object canseContract(ContractDetail entity){
		setUser(entity);
		return contractDetailService.update(entity);
	}
	
	
	/*@RequestMapping(value = "findListApps")
	@ResponseBody
	public Object findListApps(ContractDetail entity, HttpServletRequest request, HttpServletResponse response, Model model){
		Page<ContractDetail> page = new Page<ContractDetail>(request,response);
		return  contractDetailService.findListApps(page, entity);
	}*/
	@RequestMapping(value = "save")
	@ResponseBody
	public Object save(ContractDetail entity, HttpServletRequest request, HttpServletResponse response, Model model){
		setUser(entity);
		int i = StringUtils.isEmpty(entity.getId())?contractDetailService.insert(entity):contractDetailService.update(entity);
		return i;
	}
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(ContractDetail entity, HttpServletRequest request, HttpServletResponse response, Model model){
		return contractDetailService.delete(entity);
	}
	
	/**
	 * 客户认购信息审核查询列表
	 * @param entity
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findListExamine")
	@ResponseBody
	public Object findListExamine(ContractDetail entity, HttpServletRequest request, HttpServletResponse response, Model model){
		Page<ContractDetail> page = new Page<ContractDetail>(request,response);
		return  contractDetailService.findListExamine(page, entity);
	}
	
	/**
	 * 录入客户资料
	 * @param entity
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "saveCustData")
	@ResponseBody
	public Object saveCustData(ContractDetail entity,ResourceFile  resourceFile,String delids, HttpServletRequest request, HttpServletResponse response, Model model){
		setUser(entity);
		List<Resource> reslist = new ArrayList<Resource>();		
		if(resourceFile!=null){
			for(FileEntity fileEntity : resourceFile.getFileEntity()){
				if(!StringUtils.isNotBlank(fileEntity.getType())) continue;
				MultipartFile f = fileEntity.getFile();
				String path = request.getSession().getServletContext().getRealPath("/") + "static/resources/" + entity.getId() + "/" + f.getOriginalFilename() ;
				path = FileUtil.toFilePath(path);
				FileUtil.fileExistsAndfileCreate(path);
				File file = new File(path);
				try {
					f.transferTo(file);
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				String url = request.getRequestURL().toString();
				String servletPath = request.getServletPath();
				url = url.substring(0,url.length() - servletPath.length());
				url += "/static/resources/" + entity.getId() + "/" + f.getOriginalFilename();
				Resource res =new Resource();
				res.setLinkid(entity.getId());
				res.setUrl(url);
				res.setType(fileEntity.getType());
				res.setName(f.getOriginalFilename());
				reslist.add(res);
			}
			
		}
		entity.setResource(reslist);
		int i = contractDetailService.saveCustData(entity,delids);
		return i;
	}
	/**
	 * 客户认购信息录入信息提交审核
	 * @param entity
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "saveAuditData")
	@ResponseBody
	public Object saveAuditData(ContractDetail entity, HttpServletRequest request, HttpServletResponse response, Model model){
		setUser(entity);
		int i = contractDetailService.saveAuditData(entity);
		return i;
	}
	/**
	 * 提交审核状态
	 * @param entity
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "setAuditData")
	@ResponseBody
	public Object setAuditData(ContractDetail entity, HttpServletRequest request, HttpServletResponse response, Model model){
		setUser(entity);
		int i = contractDetailService.setAuditData(entity);
		return i;
	}
	/**
	 * 提交全部审核状态
	 * @param entity
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "setAllAuditData")
	@ResponseBody
	public Object setAllAuditData(ContractDetail entity, String ids, HttpServletRequest request, HttpServletResponse response, Model model){
		setUser(entity);
		int i = contractDetailService.setAllAuditData(ids);
		return i;
	}
	@RequestMapping(value = "findContractDetail")
	@ResponseBody
	public Object findContractDetail(ContractDetail entity, HttpServletRequest request, HttpServletResponse response, Model model){
		setUser(entity);
		ContractDetail  contractDetail = contractDetailService.findContractDetail(entity);
		return contractDetail;
	}
	@RequestMapping(value = "problemCansel")
	@ResponseBody
	public Object problemCansel(ContractDetail entity, HttpServletRequest request, HttpServletResponse response, Model model){
		setUser(entity);
		return contractDetailService.problemCansel(entity);
	}
	/**
	 * 认购确认书发放
	 * @param entity
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "sendContractDetail")
	@ResponseBody
	public Object sendContractDetail(ContractDetail entity,ExpressInformation  expressInformation,String ids, HttpServletRequest request, HttpServletResponse response, Model model){
		int i = contractDetailService.sendContractDetail(entity,expressInformation,ids);
		return i;
	}
}